package me.zhengjie.modules.member.rest;

import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.service.member.service.MemberSubscribeService;
import com.qiaofang.applet.service.member.service.dto.MemberDto;
import com.qiaofang.applet.service.member.service.dto.MemberSubscribeDetailDto;
import com.qiaofang.applet.service.member.service.dto.MemberSubscribeQueryDto;
import com.qiaofang.applet.service.member.service.dto.UpdateSubscribeStatusDto;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by IDEA.
 *
 * @author QF2019
 * @auther Zym.
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "其它：订阅管理")
@RequestMapping("/api/memberSubscribe")
public class MemberSubscribeController {

    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private MemberSubscribeService memberSubscribeService;

    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation(value = "查询用户订阅列表", notes = "查询用户订阅列表")
    public ResponseEntity<Page<MemberSubscribeDetailDto>> list(MemberSubscribeQueryDto memberSubscribeQueryDto, Pageable pageable) {
        memberSubscribeQueryDto.setCorporationCode(SecurityUtils.getCorporationCode());
        memberSubscribeQueryDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        Page<MemberSubscribeDetailDto> page = memberSubscribeService.listMemberSubscribe(memberSubscribeQueryDto, pageable);
        if(page != null && !CollectionUtils.isEmpty(page.getContent())) {
            Map<String, String> cityMap = cityCompanyService.getCityMap(memberSubscribeQueryDto.getCorporationCode());
            for(MemberSubscribeDetailDto memberSubscribeDetailDto : page.getContent()) {
                memberSubscribeDetailDto.setCityName(cityMap.get(memberSubscribeDetailDto.getCityCode()));
            }
        }
        return ResponseEntity.ok(page);
    }

    @PostMapping
    @ApiOperation("更新订阅状态")
    public ResponseEntity<Object> updateSubscribeStatus(@RequestBody UpdateSubscribeStatusDto updateSubscribeStatusDto){
        memberSubscribeService.updateSubscribeStatus(updateSubscribeStatusDto);
        return ResponseEntity.ok(HttpStatus.OK);
    }
}